﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MODEL;
using Service;
using GalaSoft.MvvmLight;
using System.Windows.Input;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
using Microsoft.Practices.ServiceLocation;
using Common;
using System.ComponentModel;


namespace ViewModel.AlertCreate
{
    public class AlertDocViewModel:ViewModelBase
    {
        public ICommand InsertAlertDocCommand { get; set; }

        public ICommand UpdateAlertDocComanad { get; set; }

        public ICommand DeleteAlertDocCommand { get; set; }

        private IEnumerable<AlertDoc> m_AlertDocList;
        public IEnumerable<AlertDoc> AlertDocList
        {
            get { return this.m_AlertDocList; }
            set { m_AlertDocList = value; base.RaisePropertyChanged(() => this.AlertDocList); }
        }

        public ICommand QueryAlertDocCommand { get; set; }


        private TagObjectInfo NonSelectedTag = new TagObjectInfo { TagObjectName = "未选择标签" };
        private IEnumerable<TagObjectInfo> m_TagInfoList;
        public IEnumerable<TagObjectInfo> TagInfoList
        {
            get { return this.m_TagInfoList; }
            set { this.m_TagInfoList = value; base.RaisePropertyChanged(()=>TagInfoList); }
        }

        private string NonSelectedIndustry = "未选择行业";
        private IEnumerable<string> m_IndustryList;
        public IEnumerable<string> IndustryList {
            get { return this.m_IndustryList; }
            set { this.m_IndustryList = value; base.RaisePropertyChanged(()=>IndustryList); }
        }

        private ElementInfo NonSelectedElement = new ElementInfo { ElementName = "未选择要素" };
        private IEnumerable<ElementInfo> m_ElementList;
        public IEnumerable<ElementInfo> ElementList {
            get { return m_ElementList; }
            set { this.m_ElementList = value;base.RaisePropertyChanged(() => this.ElementList); }
        }

        private TagObjectInfo m_CurrentTagInfo;
        public TagObjectInfo CurrentTagInfo
        {
            get { return m_CurrentTagInfo; }
            set { this.m_CurrentTagInfo = value; base.RaisePropertyChanged(() => CurrentTagInfo); }
        }

        private string m_CurrentIndustry;
        public string CurrentIndustry
        {
            get { return this.m_CurrentIndustry; }
            set { this.m_CurrentIndustry = value; base.RaisePropertyChanged(() => CurrentIndustry); }
        }

        private ElementInfo m_CurrentElement;
        public ElementInfo CurrentElement
        {
            get { return this.m_CurrentElement; }
            set { this.m_CurrentElement = value; base.RaisePropertyChanged(() => CurrentElement); }
        }
        
        private IAlertDocService m_AlertDocService;

        private ITagInfoService m_TagInfoService;

        private IUserService m_UserService;

        private IElementService m_ElementService;

        public AlertDocViewModel(IAlertDocService alertDocService, ITagInfoService tagInfoService, IUserService userService, IElementService elementServcie)
        {
            this.m_AlertDocService = alertDocService;
            this.m_TagInfoService = tagInfoService;
            this.m_UserService = userService;
            this.m_ElementService = elementServcie;

            this.QueryAlertDocCommand = new RelayCommand(QueryAlertDoc);
            this.UpdateAlertDocComanad = new RelayCommand<AlertDoc>(ShowUpdateAlertDocWindow);
            this.InsertAlertDocCommand = new RelayCommand(ShowInsertAlertDocWindow);
            this.DeleteAlertDocCommand = new RelayCommand<int>(DeleteAlertDoc);

            RefreshData(() => this.PropertyChanged += OnPropertyChange);
        }

        private void OnPropertyChange(object sender, PropertyChangedEventArgs e)
        {
            if (new[] { "CurrentTagInfo", "CurrentIndustry", "CurrentElement" }.Contains(e.PropertyName))
            {
                QueryAlertDoc();
            }
        }

        private async void QueryAlertDoc()
        {
            try
            {
                AlertDocQueryCriteria criteria = new AlertDocQueryCriteria();
                if (this.CurrentTagInfo != null && this.CurrentTagInfo != this.NonSelectedTag)
                {
                    criteria.TagObject = this.CurrentTagInfo.TagId.ToString();
                }

                if (!string.IsNullOrEmpty(this.CurrentIndustry) && this.CurrentIndustry != this.NonSelectedIndustry)
                {
                    criteria.UserIndustry = this.CurrentIndustry;
                }

                if (this.CurrentElement != null && this.CurrentElement != NonSelectedElement)
                {
                    criteria.ElementName = this.CurrentElement.ElementName;
                }
              
                IEnumerable<AlertDoc> alertDocs = await this.m_AlertDocService.QueryAlertDocsAsync(criteria);
                this.AlertDocList = alertDocs;
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("查询词条数据失败", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }

        private void ShowInsertAlertDocWindow()
        {
            IAlertDocService alertDocService = ServiceLocator.Current.GetInstance<IAlertDocService>();
            AlertDocEditViewModel alertDocEditViewModel = new AlertDocEditViewModel(alertDocService, this.m_UserService, this.m_TagInfoService, this.m_ElementService) { ParentViewModel = this};
            Messenger.Default.Send<AlertDocEditViewModel>(alertDocEditViewModel,"AlertDoc_ShowEditWindow");
        }

        private void ShowUpdateAlertDocWindow(AlertDoc alertDoc)
        {
            IAlertDocService alertDocService = ServiceLocator.Current.GetInstance<IAlertDocService>();
            AlertDocEditViewModel alertDocEditViewModel = new AlertDocEditViewModel(alertDocService, this.m_UserService, this.m_TagInfoService, this.m_ElementService) { AlertDoc = alertDoc ,ParentViewModel = this};

            Messenger.Default.Send<AlertDocEditViewModel>(alertDocEditViewModel, "AlertDoc_ShowEditWindow");
        }

        private async void DeleteAlertDoc(int id)
        {
            try
            {
                bool isComfirm = await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("当前数据即将被删除，是否确认？", "提示", "确认", "取消", null);
                if (!isComfirm)
                {
                    return;
                }
                Task task = this.m_AlertDocService.DeleteAlertDoc(new List<int> { id});
                task.Wait();
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("删除成功", "提示");
                RefreshData();
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("删除失败", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }

        public async void RefreshData(Action afterRefresh = null)
        {
            try
            {
                IEnumerable<TagObjectInfo> tagInfos = await this.m_TagInfoService.QueryTagInfosAsync();
                List<TagObjectInfo> tagInfoList = new List<TagObjectInfo> { this.NonSelectedTag };
                tagInfoList.AddRange(tagInfos);
                this.TagInfoList = tagInfoList;
                this.CurrentTagInfo = this.NonSelectedTag;

                IEnumerable<string> industrys = await this.m_UserService.QueryUserIndustry();
                List<string> industryList = new List<string> { NonSelectedIndustry };
                industryList.AddRange(industrys);
                this.IndustryList = industryList;
                this.CurrentIndustry = this.NonSelectedIndustry;

                IEnumerable<ElementInfo> elementInfos = await this.m_ElementService.QueryElementsAsync("自建站");
                List<ElementInfo> elementInfoList = new List<ElementInfo> { NonSelectedElement };
                elementInfoList.AddRange(elementInfos);
                this.ElementList = elementInfoList;
                this.CurrentElement = this.NonSelectedElement;

                QueryAlertDoc();

                if (afterRefresh != null)
                {
                    afterRefresh.Invoke();
                }
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("加载数据失败", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }
    }
}
